#/usr/bin/tclsh



source [file dirname [info script]]/define.tcl
set packet_size			30
set loop						10
set TEST_SW_X_ADDR 		0
set TEST_SW_Y_ADDR		1

# List all available programming hardware, and select the USB-Blaster.
# (Note: this example assumes only one USB-Blaster is connected.)
#puts "Programming Hardware:"
foreach hardware_name [get_hardware_names] {
	#puts $hardware_name
	if { [string match "USB-Blaster*" $hardware_name] } {
		set usbblaster_name $hardware_name
	}
}
#puts "\nSelect JTAG chain connected to $usbblaster_name.\n";

# List all devices on the chain, and select the first device on the chain.
#puts "\nDevices on the JTAG chain:"
foreach device_name [get_device_names -hardware_name $usbblaster_name] {
	#puts $device_name
	if { [string match "@1*" $device_name] } {
		set test_device $device_name
	}
}

# Initiate a editing sequence
begin_memory_edit -hardware_name $usbblaster_name -device_name $test_device

for {set p 1} {$p<$loop} {incr p} {		

	# update data to sent to sdram 
	puts "Sent packet to core 1 is:"
	set i 0
	set code_hex 00000000
	set inpck	0
	set err [catch {exec ./a.out 0} expect ]
	set expect_pck	$expect
	
	for {set i 1} {$i<$packet_size} {incr i} {		
		set data [expr $i*$p]
		set code_hex  [format %4.8X $data]$code_hex
		set inpck	$inpck,$data
		set err [catch {exec ./a.out $data} expect ]
		if { $i < 14 } {
			set expect_pck	$expect_pck,$expect
		} else {
			set expect_pck	$expect_pck,$data

		}
	}
	set wite "      "
	puts $wite$inpck
	
	puts "expected packet is:"
	set wite "      "
	puts $wite$expect_pck	
		write_content_to_memory -instance_index 0 -start_address 0 -word_count $packet_size -content $code_hex -content_in_hex
	
	#send input packet
	update_cmd_mem  $TEST_SW_X_ADDR $TEST_SW_Y_ADDR 0 	$packet_size		5000
	set cmd_error [send_cmd	$WRITE_CODE]
	if { $cmd_error == 1} {
			puts "------------------------------fail to send packet to core under test !----------------------------------" 
			end_memory_edit
			exit -1
	
	} 
	
	#wait for read packet
	set done 0
	set count 0
	send_cmd	$START_CODE	
	while { $done ==  0 && $count <20 } {
		set read_status  [ read_content_from_memory -instance_index 1 -start_address 0 -word_count 1 ]
		set done [string index $read_status 0]
		incr count
		send_cmd	$START_CODE
			#puts $prog_done 
			#puts $read_status
		}
		if { $count > 19} {
			puts "No packet has been recived  !" 
			end_memory_edit
			exit -1
			
		} 
	
	
	set cmd_error [send_cmd $READ_CODE]
		#set cmd_error [send_cmd	$SDRAM_RW_CODE]
	if { $cmd_error == 1} {
			puts "fail to read from core !" 
			end_memory_edit
			exit -1
	
	} 
	
	set packet_plus [expr $packet_size+1]	
	set out_pck ""
	set read_pck  [ read_content_from_memory -instance_index 0 -start_address 0 -word_count $packet_plus -content_in_hex]
	for {set i 0} {$i<($packet_plus-1)*8} {set i [expr $i+8]} {
		set d [expr $i+7]		
		set b [ string range $read_pck $i $d ]
		scan $b %x  r 
		set out_pck $r,$out_pck
	}
	puts "Recived packet is :"
	set wite "      "
	puts $wite$out_pck 
	puts ""
	puts ""
	
}	
	
end_memory_edit



